package com.mashibing.serviceorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.serviceorder.remote.ServicePriceClient;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.constant.OrderConstants;
import org.mashibing.dto.OrderInfo;
import org.mashibing.dto.ResponseResult;
import org.mashibing.request.OrderRequest;
import org.mashibing.request.PriceRuleIsNewRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.mashibing.serviceorder.mapper.OrderInfoMapper;
import com.mashibing.serviceorder.service.OrderInfoService;

/**
 * @author: $dujunhua
 * @create: 2024/7/5 10:49
 * @description:
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    ServicePriceClient servicePriceClient;
    @Override
    public ResponseResult add(OrderRequest orderRequest) {
        // 需要判断计价规则的版本是否为最新
        PriceRuleIsNewRequest priceRuleIsNewRequest = new PriceRuleIsNewRequest();
        priceRuleIsNewRequest.setFareType(orderRequest.getFareType());
        priceRuleIsNewRequest.setFareVersion(orderRequest.getFareVersion());
        ResponseResult<Boolean> aNew = servicePriceClient.isNew(priceRuleIsNewRequest);
        if (!(aNew.getData())){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_CHANGED.getCode(),CommonStatusEnum.PRICE_RULE_CHANGED.getValue());
        }

        // 创建订单
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(orderRequest, orderInfo);
        orderInfo.setOrderStatus(OrderConstants.ORDER_START);
        this.baseMapper.insert(orderInfo);
        return ResponseResult.success();
    }
}
